import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import javax.swing.*;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

public class Main {
    public static void main(String[] args) throws InterruptedException {
        //test01();
//        test02();
//        test03();
//        test04();
//        test05();
//        test06();
//        test07();
//        test08();
//        test09();
//        test10();
//        test11();
//        test12();
//        page01();
//        page02();
        page03();
    }
    private static void page03() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/%E8%87%AA%E5%8A%A8%E5%8C%96%E6%B5%8B%E8%AF%95/src/main/page/test03.html?_ijt=9pasp68et271uciai5dath4ce2&_ij_reload=RELOAD_ON_SAVE");
        WebElement element = webDriver.findElement(By.cssSelector("#ShippingMethod"));
        Select select = new Select(element);
        //1.下标定位，从0下标开始
        select.selectByIndex(3);
//        //2.值定位，属性的value值
//        select.selectByValue("9.25");
        
    }

    private static void page02() {
         WebDriver webDriver = new ChromeDriver();
         //1.打开页面
         webDriver.get("http://localhost:63342/%E8%87%AA%E5%8A%A8%E5%8C%96%E6%B5%8B%E8%AF%95/src/main/page/test02.html?_ijt=6m5kegbg0ot305b8c0ac69he26&_ij_reload=RELOAD_ON_SAVE");
         //获取frame中的属性
         webDriver.switchTo().frame("f1");
        //选择click按钮，点击
         webDriver.findElement(By.cssSelector("body > div > div > a")).click();

    }

    //定位多组元素
    private static void page01() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/%E8%87%AA%E5%8A%A8%E5%8C%96%E6%B5%8B%E8%AF%95/src/main/page/test01.html?_ijt=s0olpsodehg4mc180oe9t5rddm&_ij_reload=RELOAD_ON_SAVE");
        List<WebElement> webElements = webDriver.findElements(By.cssSelector("input"));
        for (int i = 0; i < webElements.size(); i++) {
            if (webElements.get(i).getAttribute("type").equals("checkbox")) {
                webElements.get(i).click();
            }
            //否则啥也不做
        }

    }

    //
    //鼠标事件
    private static void test12() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        //打开百度首页
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("清明节习俗");
        sleep(3000);//强制等待3秒
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        //找到视频按钮
        WebElement element = webDriver.findElement(By.cssSelector("#s_tab > div > a.s-tab-item.s-tab-item_1CwH-.s-tab-video_1Sf_u.s-tab-video"));
        sleep(3000);
        //
        Actions action = new Actions(webDriver);
        action.moveToElement(element).contextClick().perform();
    }


    //键盘事件
    private static void test11() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        //打开百度首页
        webDriver.get("https://www.baidu.com/");
//        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.ENTER);
//        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.SPACE);
//        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.ESCAPE);
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("清明节习俗");
        sleep(3000);
        //ctrl+A 全选
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"A");
        sleep(3000);
        //ctrl+X 剪切
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"X");
        sleep(3000);
        //ctrl+V 粘贴
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"V");
    }

    //设置浏览器最大化及设置浏览器宽、高
    private static void test10() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        //打开百度首页
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("清明节习俗");
        sleep(3000);//强制等待3秒
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        //最大化
        webDriver.manage().window().maximize();
        sleep(3000);
        //全屏
        webDriver.manage().window().fullscreen();
        sleep(3000);
        //设置长宽
        webDriver.manage().window().setSize(new Dimension(300,600));
    }

    //滚动条操作
    private static void test09() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        //打开百度首页
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("清明节习俗");
        sleep(3000);//强制等待3秒
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        ((JavascriptExecutor) webDriver).executeScript("document.documentElement.scrollTop=10000");
    }

    //浏览器操作
    private static void test08() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("清明节习俗");
        sleep(3000); //使用强制等待让我们的结果可视化
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        //后退
        webDriver.navigate().back();
        sleep(3000);
        //刷新
        webDriver.navigate().refresh();
        sleep(3000);
        //前进
        webDriver.navigate().forward();
        sleep(3000);
        //滚动条滚到最低端
        ((JavascriptExecutor)webDriver).executeAsyncScript("document.documentElement.scrollTop=10000");


    }


    //显式等待
    private static void test07() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        WebDriverWait wait = new WebDriverWait(webDriver,1);
        //判断某个元素是否可以被点击
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#bottom_layer > div > p:nth-child(8)")));
        //判断页面的标题
        wait.until(ExpectedConditions.titleIs("百度一下，你就知道"));
    }


     //隐式等待
    private static void test06() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("什么是软件测试");
        webDriver.findElement(By.cssSelector("#su")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.DAYS);
        //删除百度搜索框里的内容
        webDriver.findElement(By.cssSelector("#kw")).clear();
    }

    //强制等待
    private static void test05() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        sleep(3000);
        webDriver.findElement(By.cssSelector(".s_ipt")).sendKeys("什么是软件测试");
        sleep(3000);
        webDriver.findElement(By.cssSelector("#su")).click();

    }

    private static void test01() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        //允许所有请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver( options);
        webDriver.get("https://www.baidu.com");
        //WebElement element = webDriver.findElement(By.cssSelector(".s_ipt")); //通过类选择器
        WebElement element = webDriver.findElement(By.cssSelector("#kw"));   //通过id选择器
        //WebElement element = webDriver.findElement(By.xpath("//*[@id=\"kw\"]"));
        element.sendKeys("软件测试");
        // 找到百度一下按钮
        // 点击
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        element.clear();
    }

    private static void test02() {
        ChromeOptions options = new ChromeOptions();
        //允许所有请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver( options);
        webDriver.get("https://www.baidu.com");
        WebElement element = webDriver.findElement(By.cssSelector("#kw"));
        //element.sendKeys("软件测试");
//        WebElement element1 = webDriver.findElement(By.cssSelector("#su"));
        webDriver.findElement(By.xpath("//a [text()=\"新闻\"]")).submit();

    }
    private static void test03() {
        ChromeOptions options = new ChromeOptions();
        //允许所有请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver( options);
        webDriver.get("https://www.baidu.com");
        String button_value = webDriver.findElement(By.cssSelector("#su")).getAttribute("value");
        if (button_value.equals("百度一下")) {
            System.out.println("测试通过");
        } else {
            System.out.println("测试不通过");
        }
    }
    private static void test04() {
        ChromeOptions options = new ChromeOptions();
        //允许所有请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver( options);
        webDriver.get("https://www.baidu.com/");
       String url = webDriver.getCurrentUrl();
       String title = webDriver.getTitle();
       if (url.equals("https://www.baidu.com/") && title.equals("百度一下，你就知道")) {
           System.out.println("测试通过 url= " + url + " title: " + title);
       } else {
           System.out.println("测试不通过");
       }
    }

}
